﻿using System;
using System.Linq;
namespace Javavaj.iHunter.Core
{
    public static class HashExtentions
    {
        /// <summary>
        /// 计算频道的Hash码
        /// </summary>
        /// <param name="Channel">需要计算Hash码的频道</param>
        /// <returns>32位的整数Hash码</returns>
        public static int ComputeHash(this IChannel channel)
        {
            return (channel.GetType().FullName).GetHashCode() ^ channel.GetHashCode();
        }

        public static int ComputeHash(this IMessage message)
        {
            return (message.ChannelHash) ^ message.Uid;
            
        }

        public static bool SameAs(this IMessage message, IMessage other)
        {
            if (message.Uid != other.Uid)
                return false;

            if (
                message.Author != other.Author||
                message.Category != other.Category||
                message.ContentType != other.ContentType||
                message.Description != other.Description||
                message.DueTime != other.DueTime||
                message.Importance != other.Importance||
                message.Link != other.Link||
                message.PubTime != other.PubTime||
                message.Repliable != other.Repliable||
                message.Title != other.Title ||
                message.ContentIsBinary != other.ContentIsBinary
                )
            {
                return false;
            }

            if (message.ContentIsBinary)
            {
                byte[] thisContent = message.Content as byte[];
                byte[] otherContent = other.Content as byte[];
                if (thisContent.Length != otherContent.Length)
                    return false;
                for (int i = 0; i < thisContent.Length; i++)
                {
                    if (thisContent[i] != otherContent[i])
                        return false;
                }
            }
            else
            {
                string thisContent = message.Content as string;
                string otherContent = other.Content as string;
                if (thisContent != otherContent)
                    return false;
            }

            if (message.Attachments.Count != other.Attachments.Count)
                return false;

     
            for (int i = 0; i < message.Attachments.Count; i++)
            {
                IFileObject thisAttachment = message.Attachments[i];
                IFileObject otherAttachment = other.Attachments[i];

                if (!thisAttachment.SameAs(otherAttachment))
                    return false;
                
            }
            return true;

                 
        }

        public static bool SameAs(this IFileObject file, IFileObject other)
        {
            if (file.Filename != other.Filename ||
                file.FileSize != other.FileSize ||
                file.FileType != other.FileType ||
                file.FileUri != other.FileUri
                )
            {
                return false;
            }
            return true;
        }

        public static int ComputeHash(this IFileObject file)
        {
            return (file.Filename + file.Description + file.FileSize.ToString()).GetHashCode();
        }
    }
}